package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "downOrUp")
    public void downOrUp(String message){
        if (StringUtils.isBlank(message)){
            log.warn("消息为空");
            return;
        }

        Map map = JSON.parseObject(message, Map.class);
        Long articleId = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");
        LambdaQueryWrapper<ApArticleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleConfig::getArticleId,articleId);
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(queryWrapper);
        if (apArticleConfig == null){
            return;
        }
        apArticleConfig.setIsDown(enable == 0);
        apArticleConfigMapper.updateById(apArticleConfig);
    }

}
